package org.springcloud.wisdom.spring_02_classroom.admin.createcourse.dao;

import org.apache.ibatis.annotations.*;
import org.springcloud.wisdom.spring_02_classroom.admin.createcourse.entity.Course;

import java.util.List;

@Mapper
public interface CourseDao {
    //开启选课
    @Update("UPDATE course " +
            "SET  iscanlook =1 ")
    int updatecan(Course course);

    //关闭选课
    @Update("UPDATE course " +
            "SET  iscanlook =0 ")
    int updatecant(Course course);

    @Select("SELECT MAX(cid) FROM course")
    String getMaxCid();

    // 管理员创建课程
    @Insert("INSERT INTO course (cid, cname, tid, ltime, location, chour) " +
            "VALUES (#{cid}, #{cname}, #{tid}, #{ltime}, #{location}, #{chour})")
    int insertCourse(Course course);

    // 管理员可以通过id修改上课信息
    @Update("UPDATE course " +
            "SET  cname=#{cname},tid=#{tid},ltime=#{ltime}, location=#{location}, chour=#{chour} " +
            "WHERE cid=#{cid}")
    int updateCourse(Course course);

    // 管理员删除课程
    @Update(" UPDATE course SET isdel = #{isdel} WHERE cid = #{cid}")
    int deleteCourse(@Param("cid") String cid, @Param("isdel") boolean isdel);

    //分页有条件查课程
    @Select("<script>" +
            "SELECT c.*, t.tname FROM course c " +
            "INNER JOIN teacher t ON c.tid = t.tid " +
            "<where> " +
            "    <if test=\"cid != null\">" +
            " <bind name=\"Key\" value=\"'%'+cid+'%'\"/>"+
            "                and        cid like #{Key} " +
            "    </if>" +
            "    <if test=\"tid != null\">" +
            " <bind name=\"Key\" value=\"'%'+tid+'%'\"/>"+
            "                and        tid like #{Key} " +
            "    </if>" +
            "  AND c.isdel = 0 " +
            "</where> " +
            "LIMIT #{start}, #{size}" +
            "</script>")
    public List<Course> get(@Param("cid") String cid,@Param("tid") String tid,
                                      @Param("start") int start, @Param("size") int size);

    @Select("<script>" +
            "SELECT count(*) FROM course " +
            "<where> " +
            "    <if test=\"cid != null\">" +
            " <bind name=\"Key\" value=\"'%'+cid+'%'\"/>"+
            "                and        cid like #{Key} " +
            "    </if>" +
            "    <if test=\"tid != null\">" +
            " <bind name=\"Key\" value=\"'%'+tid+'%'\"/>"+
            "                and        tid like #{Key} " +
            "    </if>" +
            "  AND isdel = 0 " +
            "</where> " +
            "</script>")
    public int Count(  String cid ,String tid);


    // 检查特定教师和时间地点组合是否冲突
    // 在CourseDao中添加一个新参数currentCid到相应的方法中
    @Select("SELECT COUNT(*) FROM course WHERE tid = #{tid} AND ltime = #{ltime} AND location = #{location} AND cid != #{currentCid} AND isdel = 0")
    int checkTimeLocationConflict(@Param("tid") String tid, @Param("ltime") String ltime, @Param("location") String location, @Param("currentCid") String currentCid);

    // 检查特定教室和时间是否已被占用
    @Select("SELECT COUNT(*) FROM course WHERE location = #{location} AND ltime = #{ltime} AND cid != #{currentCid} AND isdel = 0")
    int checkLocationTimeConflict(@Param("location") String location, @Param("ltime") String ltime,@Param("currentCid") String currentCid);

    // 检查特定教师和时间是否冲突
    @Select("SELECT COUNT(*) FROM course WHERE tid = #{tid} AND ltime = #{ltime} AND cid != #{currentCid} AND isdel = 0")
    int checkTidTimeConflict(@Param("tid") String tid, @Param("ltime") String ltime,@Param("currentCid") String currentCid);

    //历史
    //分页有条件查课程
    @Select("<script>" +
            "SELECT c.*, t.tname FROM course c " +
            "INNER JOIN teacher t ON c.tid = t.tid " +
            "<where> " +
            "    <if test=\"cid != null\">" +
            " <bind name=\"Key\" value=\"'%'+cid+'%'\"/>"+
            "                and        cid like #{Key} " +
            "    </if>" +
            "    <if test=\"tid != null\">" +
            " <bind name=\"Key\" value=\"'%'+tid+'%'\"/>"+
            "                and        tid like #{Key} " +
            "    </if>" +
            "  AND c.isdel = 1 " +
            "</where> " +
            "LIMIT #{start}, #{size}" +
            "</script>")
    public List<Course> getH(@Param("cid") String cid,@Param("tid") String tid,
                            @Param("start") int start, @Param("size") int size);

    @Select("<script>" +
            "SELECT count(*) FROM course " +
            "<where> " +
            "    <if test=\"cid != null\">" +
            " <bind name=\"Key\" value=\"'%'+cid+'%'\"/>"+
            "                and        cid like #{Key} " +
            "    </if>" +
            "    <if test=\"tid != null\">" +
            " <bind name=\"Key\" value=\"'%'+tid+'%'\"/>"+
            "                and        tid like #{Key} " +
            "    </if>" +
            "  AND isdel = 1 " +
            "</where> " +
            "</script>")
    public int CountH(  String cid ,String tid);
}

